 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=84 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:04 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Avoid JavaBeans style of construction
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='API,immutable,invariant,mutability,mutable,serializable,set,specification,constructor,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Avoid JavaBeans style of construction</div>

<div class='main-body'>
 
<br>Some argue that to construct a <a href='TopicActiond08d-2.html'>Model Object</a> in many steps, first by calling
a no-argument constructor, and then by calling a series of <tt>setXXX</tt>
methods, is something to be avoided, if possible :
<ul>
<li>
it is more complex, since one call is replaced with many</li>

<li>
there is no simple way to ensure that all necessary <tt>setXXX</tt> methods
are called</li>

<li>
there is no simple way to implement a <a href="TopicAction0d31-2.html">class invariant</a></li>

<li>
it allows the object to be in intermediate states which are not immediately
useful, and perhaps even invalid</li>

<li>
it is not compatible with the (very useful) <a href="TopicActiond838-2.html">immutable
object</a> pattern</li>

<li>
it does not follow the style of many design patterns, in which concrete
classes usually have arguments passed to their constructor. This is a recurring
theme in the <i>Design Patterns</i> book. Constructors usually act as a
"back door" for data which is needed by an implementation, but which cannot
appear in the signature of some higher-level abstraction (since it would
pollute such an abstraction with data specific to an implementation).</li>
</ul>
Of course, JavaBeans follow this pattern of "no-argument constructor plus
<tt>setXXX</tt>".
However, JavaBeans were originally intended for a very narrow problem domain - manipulation 
of graphical components in an IDE.
<span class='highlight'>As a model for the
construction of typical data-centric objects (Model Objects), JavaBeans
seem completely inappropriate</span>. One can even argue strongly that, for Model Objects,
the JavaBeans style is an <i>anti-pattern</i>, and should be avoided unless
absolutely necessary.

<P><b><em>"The JavaBeans pattern has serious disadvantages."</em> - Joshua Bloch, <em>Effective Java</em></b>

<P>
<hr width="30%">

If you need more convincing, read on.

<P>Here is the definition of a Java Bean, directly from the <a href='http://java.sun.com/products/javabeans/docs/spec.html'>specification</a> :
<P><b>"A Java Bean is a reusable software component that can be manipulated visually
in a builder tool."</b>

<P>This is very clear and simple. No mention is made here, or anywhere else in the 
specification, of Model Objects used to encapsulate database records. The whole idea 
of a Model Object is entirely absent from the Java Bean specification, and for good reason: the manipulation of 
graphical controls and the representation of database records are unrelated issues.

<P>The life cycle of a Java Bean, starting with its no-argument constructor, is derived mainly from 
the task of <em>configuring</em> a graphical control having some default initial state. 
For the intended domain, this is indeed a reasonable design. 
For <a href='TopicActiond08d-2.html'>Model Objects</a>, however, the idea of <em>configuration</em> in this sense is entirely meaningless. 

<P>The role of a Model Object is to carry data. Since a no-argument constructor can only build an object that is initially 
empty, then it cannot contribute directly to this aim. It does not form a natural candidate for the 
design of a Model Object.

<P>To put data into empty Java Beans, frameworks typically call various <tt>setXXX</tt> methods using reflection. 
There is nothing to prevent them from calling constructors in exactly the same way. 

<P>A demonstration that a framework does <em>not</em> need JavaBeans for its Model Objects is 
found in the <a href='TopicAction5f31-2.html'>WEB4J</a> tool. WEB4J calls (surprise, surprise) a constructor to 
build Model Objects. It recommends implementing Model Objects as <a href='TopicActiond838-2.html'>immutable objects</a>, 
not Java Beans.

<P>The widespread adoption of Java Beans as a supposedly appropriate design for Model Objects seems to be an error. 
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicAction0d31-2.html?Id=6'>Validate state with class invariants</a> <br>
 
  
  <a href='TopicActiond838-2.html?Id=29'>Immutable objects</a> <br>
 
  
  <a href='TopicActiond08d-2.html?Id=187'>Model Objects</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='84'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=84 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:04 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
